package com.yilin.tms.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.commons.util.ArrayUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.entity.message.Bulletin;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.message.mapper.BulletinMapper;
import com.yilin.tms.message.service.IBulletinService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

/**
 * 公告服务
 *
 * @author zhangjinyuan
 */
@Service
@Transactional
public class BulletinService extends ServiceImpl<BulletinMapper, Bulletin> implements IBulletinService {

    @Resource
    private BulletinMapper bulletinMapper;

    @Override
    public PageData<Bulletin> getBulletinPage(Account loginUser, Bulletin query, Integer page, Integer limit) {
        PageData<Bulletin> pageQuery = new PageData<>(page, limit);
        PageData<Bulletin> pageData = bulletinMapper.getBulletinPage(pageQuery, loginUser, query);
        return pageData;
    }

    @Override
    public List<Bulletin> getBulletinListByIds(List<String> initList) {
        return bulletinMapper.selectBatchIds(initList);
    }

    @Override
    public Boolean judgmentBulletin(Account loginUser, Integer num) {
        QueryWrapper<Bulletin> bulletinQueryWrapper = new QueryWrapper<>();
        bulletinQueryWrapper.eq("zone_plat_id", loginUser.getOrganizeId());
        bulletinQueryWrapper.eq("revealed", true);
        List<Bulletin> bulletinList = bulletinMapper.selectList(bulletinQueryWrapper);
        if (ArrayUtil.isNotEmpty(bulletinList)) {
            if (bulletinList.size() > 10 - num) {
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }

    }
}
